<template>
	<view>
		<view @click="onUpload" class="pdf1">
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			classify: {
				type: String,
				default: '0503'
			},
			troubleId: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				test: '',
				files: {
					fileName: '', //文件名
					uuid: '',
				},
				urls: './../../static/iconPdf1.png',
				flag: true
			}
		},
		mounted() {
			this.fn()
		},
		watch: {
			// "files.fileName":{
			// 	handler: function() {
			// 		this.pdf()
			// 	},
			// }
		},
		methods: {
			fn() {
				console.log(this.troubleId, 'this.troubleId')
				this.files.uuid = this.troubleId ? this.troubleId : this.guid()
				this.$emit('file', this.files)
			},
			//预览
			// async pdf() {
			// 	let params = {
			// 		connectId:this.files.uuid,
			// 		fileName:this.files.fileName,
			// 		classify:this.classify
			// 	}
			// 	let res = await this.request(this.$api.getDownloadUrl, params, 'POST')
			// 	this.urls = res.resultValue
			// },
			handleWXUpload(file) {
				console.log(file)
				wx.getFileSystemManager().readFile({
					filePath: file.path,
					encoding: 'base64',
					success: (res) => {
						let data = [{
							connectId: this.files.uuid,
							classify: this.classify,
							fileName: file.name,
							content: res.data
						}]
						this.request(this.$api.upload, data, 'POST').then(res1 => {
							console.log(res1)
							wx.showToast({
								icon: "success",
								title: "上传成功"
							})
						})
						console.log(data)
					},
					fail: (error) => {
						console.log('error', error)
					}
				})
			},
			//选择附件
			onUpload() {
				if (this.flag) {
					this.flag = false
					let time = setTimeout(() => {
						clearTimeout(time)
						this.flag = true
					}, 1000)
					let that = this
					// #ifdef MP-WEIXIN
					wx.chooseMessageFile({
						count: 1,
						type: 'all',
						success: ({
							tempFiles
						}) => {
							that.handleWXUpload(tempFiles[0]);
						},
					});
					// #endif
					// #ifdef APP-PLUS
					var styles = {};
					let token = uni.getStorageSync('token')
					let wvPath = '/hybrid/html/index.html?token=' + token + '&uuid=' + this.files.uuid + '&classify=' + this.classify
					let currentWebview = this.$mp.page.$getAppWebview()
					let wv = plus.webview.create('', wvPath, {
						'uni-app': 'none', //不加载uni-app渲染层框架，避免样式冲突
						width: '100%',
						height: '105%',
						left: '0px',
						right: '0px',
						top: '0px',
						bottom: '-20px',
						zindex: 999,
						position: 'absolute',
						background: 'transparent'
					});
					wv.loadURL(wvPath);
					currentWebview.append(wv);
					// this.test = currentWebview
					wv.overrideUrlLoading({mode: 'reject'}, (e) => {
						
					})
					plus.globalEvent.addEventListener('plusMessage', function(msg) {
						// 通过查看 msg 可以看到 api 传递的参数
						// name 为 webview的html页面中uni。调用的api，
						// 比如 uni.navigateTo， 那么此处的 name 是 navigateTo，
						// 比如 uni.postMessage， 那么此处的 name 是 postMessage，
						// 此处 url 为 html中 uni 调用 api 时的 url
						if (msg.data.args.data.name == 'postMessage') {
							console.log(111)
							that.files = {
								fileName: msg.data.args.data.arg.fileName,
								uuid: msg.data.args.data.arg.uuid
							}
							that.$emit('file', that.files)
							if (that.files.fileName !== '') {
								uni.showToast({
									icon: "success",
									title: "上传成功"
								})
							}
						}
					});
					// #endif
				}
			}
		}
	}
</script>

<style scoped>
	.pdf1 {
		margin-top: 30rpx;
		width: 100rpx;
		height: 100rpx;
		background-size: 100%;
		background-repeat: no-repeat;
		background-image: url(../../static/iconPdf1.png);
	}
</style>
